var express = require('express')
var router = express.Router()
var config = require('../../config.json')
var localRouter = require('./local')
var remoteRouter = require('./remote')
var seekerRouter = require('./seeker')
var muilter = require('../../multerUtil')
var path = require('path')
var upload = muilter.single('file')
var $ = require('../util')
var fs = require('fs')

router.all('/service/app/getConfigJson', (req, res) => {
  res.send($.readJson('server/config.json'))
})

router.all('/service/app/setConfigJson', (req, res) => {
  // 关闭自动登录时，清除登陆状态
  if (!req.body.auto_login) {
    $.cookie = ''
  }
  $.writeFile(req.body, 'server/config')
  res.send({
    result: 'success'
  })
})

// express.router，当有多个一样的url时，第一个生效
// 所以，这里希望localData优先级更高，就把localRouter放在第一个
// 没有routes-data文件夹时创建
if (!fs.existsSync('./server/routes-data')) {
  fs.mkdirSync('./server/routes-data')
}
localRouter(router)
// 远程转发请求router
remoteRouter(router)
// 没有seeker-data文件夹时创建，防止启动报错
if (!fs.existsSync('./server/seeker-data')) {
  fs.mkdirSync('./server/seeker-data')
}
seekerRouter(router)


module.exports = router
